//
//  ZDBAlertHelper.m
//  ZDB
//
//  Created by 微笑吧阳光 on 2020/3/19.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBAlertHelper.h"
#import "ExchangeMakeSurePopView.h"
#import "ExchangeResultPopView.h"
#import "ZDBSharePopShowView.h"

@interface ZDBAlertHelper ()<UIGestureRecognizerDelegate>
@property(nonatomic,strong)ZDBSharePopShowView * contentVShowView;

@end
@implementation ZDBAlertHelper


//创建单例
+ (ZDBAlertHelper *) sharedInstance
{
    static  ZDBAlertHelper *sharedInstance = nil ;
    static  dispatch_once_t onceToken;
    dispatch_once (& onceToken, ^ {
        //初始化自己
        sharedInstance = [[self alloc] init];
    });
    return  sharedInstance;
}


-(void)alert_PopExchangeInfoWith:(NSDictionary *)infoDicc andActionBlock:(void (^)(NSInteger actionType,NSDictionary * resultDicc))action{
    
    if (kDictIsEmpty(infoDicc)) {
        return;
    }
    ExchangeMakeSurePopView * contentView = [[NSBundle mainBundle]loadNibNamed:@"ExchangeMakeSurePopView" owner:self options:nil].lastObject;
    contentView.gk_size = CGSizeMake(kScreenWidth, 260);
    contentView.infoDicc = infoDicc;
    
    
    [GKCover coverFrom:[UIApplication sharedApplication].keyWindow
           contentView:contentView
                 style:GKCoverStyleTranslucent
             showStyle:GKCoverShowStyleBottom
         showAnimStyle:GKCoverShowAnimStyleBottom
         hideAnimStyle:GKCoverHideAnimStyleBottom
              notClick:NO];
    
        [contentView.sureBtn add_BtnClickHandler:^(NSInteger tag) {
           [GKCover hideCover];
        if (action) {
            action(200,infoDicc);
        }
    }];
    
    [contentView.editBtn add_BtnClickHandler:^(NSInteger tag) {
           [GKCover hideCover];
        if (action) {
            //编辑地址
            action(300,infoDicc);
        }
    }];
    
    [contentView.closeBtn add_BtnClickHandler:^(NSInteger tag) {
        [GKCover hideCover];
       if (action) {
                   action(400,[NSDictionary dictionary]);
               }
    }];

    
}

//兑换弹出成功订单
-(void)alert_PopExchangeReslutsWithActionBlock:(void (^)(NSInteger actionType))action{
    
    ExchangeResultPopView * contentView = [[NSBundle mainBundle]loadNibNamed:@"ExchangeResultPopView" owner:self options:nil].lastObject;
    contentView.gk_size = CGSizeMake(260, 260);
    
    KViewRadius(contentView, 10);
    
    [GKCover coverFrom:[UIApplication sharedApplication].keyWindow
           contentView:contentView
                 style:GKCoverStyleTranslucent
             showStyle:GKCoverShowStyleCenter
         showAnimStyle:GKCoverShowAnimStyleCenter
         hideAnimStyle:GKCoverHideAnimStyleCenter
              notClick:YES showBlock:^{
                  NSLog(@"遮罩显示了。。。");
              } hideBlock:^{
                  NSLog(@"遮罩消失了。。。");
              }];
    
        [contentView.backBtn add_BtnClickHandler:^(NSInteger tag) {
           [GKCover hideCover];
        if (action) {
            action(201);
        }
    }];
    
    [contentView.lookOrderBtn add_BtnClickHandler:^(NSInteger tag) {
        [GKCover hideCover];
       if (action) {
                   action(202);
               }
    }];
    
}
#pragma mark - 滚动条中图片的手势事件

-(void)toADDeatil:(UITapGestureRecognizer *)tap{
 
     

    
}
//分享
-(void)alert_PopEShareWith:(NSDictionary *)shareDicc isNmuberShoare:(BOOL)isNumber isInvitationShoare:(BOOL)isInvitation andActionBlock:(void (^)(NSInteger actionType))action{
    
  ZDBSharePopShowView * contentView = [[NSBundle mainBundle]loadNibNamed:@"ZDBSharePopShowView" owner:self options:nil].lastObject;
  contentView.gk_size = CGSizeMake(kScreenWidth-70, 425);
    if (isInvitation) {
        UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, contentView.width-20, 36)];
        lable.textColor = WHITECOLOR;
        lable.font = AutoBoldFont(30);
        lable.text  = @"邀好友赢大奖";
        lable.textAlignment = NSTextAlignmentCenter;
        [contentView addSubview:lable];
        
        UILabel * profitLable = [[UILabel alloc] initWithFrame:CGRectMake(10, lable.bottom +10, contentView.width-20, 21)];
        profitLable.textColor = ColorStr(@"FBF14A");
        profitLable.font = AutoBoldFont(21);
        profitLable.text  = @"得40元红包 88积分";
        profitLable.textAlignment = NSTextAlignmentCenter;
        [contentView addSubview:profitLable];
        contentView.titleImage.hidden = YES;
        contentView.shareIcon.image = [UIImage imageNamed:@"extension_bg"];
        
    }
  KViewRadius(contentView.popBg, 5);
  UIImage * codeImage =  [self createNonInterpolatedUIImageFormCIImage:[self creatQRcodeWithUrlstring:[shareDicc jsonString:@"shareUrl"]] withSize:150];
  contentView.codeImage.image = codeImage;
  
    if (isNumber==YES) {
      contentView.labHC.constant = 18;
      contentView.topHC.constant = 15;
      contentView.contentLab.hidden = NO;
    }else{
        contentView.labHC.constant = 0;
              contentView.topHC.constant = 0;
              contentView.contentLab.hidden = YES;
      
    }
    
//    UITapGestureRecognizer *imgTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toADDeatil:)];
//    imgTap.delegate=self;
//    //打开UIImageView的用户交互
//     contentView.codeImage.userInteractionEnabled=YES;
//     [contentView.codeImage addGestureRecognizer:imgTap];
    WEAKSELF
    [contentView.codeBtn add_BtnClickHandler:^(NSInteger tag) {
        STRONGSELFFor(weakSelf);
        contentView.closeBtn.hidden = YES;
        dispatch_async(dispatch_get_main_queue(), ^{
            
            UIImage * snapShotImage =[strongSelf getImageViewWithView:contentView.popBg ];
            
                   if (snapShotImage != nil) {
                        UIImageWriteToSavedPhotosAlbum(snapShotImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
                      }
            contentView.closeBtn.hidden = NO;
                     });
    }];
    //累计收入:¥0.00元  邀请0人
    contentView.contentLab.text = [NSString stringWithFormat:@"累计收入:%@ 邀请%@人",[shareDicc jsonString:@"shareMoney"],[shareDicc jsonString:@"shareCount"]];
    self.contentVShowView = contentView;
    [GKCover coverFrom:[UIApplication sharedApplication].keyWindow
              contentView:contentView
                    style:GKCoverStyleTranslucent
                showStyle:GKCoverShowStyleCenter
            showAnimStyle:GKCoverShowAnimStyleCenter
            hideAnimStyle:GKCoverHideAnimStyleCenter
                 notClick:YES showBlock:^{
                     NSLog(@"遮罩显示了。。。");
                 } hideBlock:^{
                     NSLog(@"遮罩消失了。。。");
                 }];
    
    [contentView.closeBtn add_BtnClickHandler:^(NSInteger tag) {
           [GKCover hideCover];
   
     }];

}
- (CIImage *)creatQRcodeWithUrlstring:(NSString *)urlString{
    
    // 1.实例化二维码滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
    [filter setDefaults];
    // 3.将字符串转换成NSdata
    NSData *data  = [urlString dataUsingEncoding:NSUTF8StringEncoding];
    // 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
    [filter setValue:data forKey:@"inputMessage"];
    // 5.生成二维码
    CIImage *outputImage = [filter outputImage];
    return outputImage;
}
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}

- (UIImage *)getImageViewWithView:(UIView *)view{
    UIGraphicsBeginImageContext(view.frame.size);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
    UIImage *image =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
    
}
- (void)loadImageFinished:(UIImage *)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}
 
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
    NSString *msg = nil ;
     if(error){
         msg = @"保存图片失败" ;
         [JMNotifyView showNotify:msg isSuccessful:NO];
     }else{
         msg = @"保存图片成功,可从相册选择发送给好友" ;
         [JMNotifyView showNotify:msg isSuccessful:YES];
     }
    
}

@end
